package com.muc.socket;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import com.muc.view.MainView;

public class TcpClient {
    Socket cs;
    PrintWriter os;
    private String line[];
    private static MainView gui;//主操作
    public TcpClient() throws IOException {
        try {
                cs = new Socket("127.0.0.1", 6666);//创建一个Client socket并绑定端口号9999
                os = new PrintWriter(cs.getOutputStream(), true);//打开输出流
            } catch (UnknownHostException e) {
                System.err.println("Failed to get Server hostname");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Failed to create socket");
                System.exit(1);
            }
            //由Socket对象获得输出流，并创建PrintWriter对象
            PrintWriter os = new PrintWriter(cs.getOutputStream());
            //由Socket对象获得输入流，并创建BufferedReader对象
            BufferedReader is = new BufferedReader(new InputStreamReader(cs.getInputStream()));
            //创建标准输入BufferedReader对象
            //BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            int typeFlag=0;
            boolean flag=true;
            line=new String[3];
            //line[0] 存的是返回的要操作的车辆ID
            line[0]=line[1]=line[2]="";
            while (true){
                String[] lines = new String[2];
                lines[0] = lines[1] = "";
                while (gui.isAdd() || gui.isDel() || gui.isUpdate() || gui.isSearch() || gui.isPrePage() || gui.isNextPage()) {
                    if (gui.isAdd()) {//给服务器发送增加指令
                        lines[0] = "删除";
                        lines[1] = "#-1";
                        gui.setAdd(false);

//                            channel.write(ByteBuffer.wrap(lines[0].getBytes()));
//                            channel.write(ByteBuffer.wrap(lines[1].getBytes()));
//                            channel.write(ByteBuffer.wrap("删除".getBytes()));
                    } else if (gui.isDel()) {//删除可以删除多行
                        lines[0] = "删除";
                        for (int carId : gui.getSelectedCarIds()) {
                            lines[1] = "#" + carId;//# 做分隔符 spilt切割
                        }
                        gui.setDel(false);
                    } else if (gui.isUpdate()) {//修改只能一次改一行
                        lines[0] = "修改";
                        int[] selectedCarIds = gui.getSelectedCarIds();
                        lines[1] = selectedCarIds[0] + "";
                        gui.setUpdate(false);
                    } else if (gui.isSearch()) {
                        lines[0] = "查询";
                        lines[1] = "#-1";
                        gui.setSearch(false);
                    } else if (gui.isPrePage()) {
                        lines[1] = "#-1";
                        lines[0] = "上一页";
                        gui.setPrePage(false);
                    } else if (gui.isNextPage()) {
                        lines[0] = "下一页";
                        lines[1] = "#-1";
                        gui.setNextPage(false);
                    }
               /* while(flag)
                {
                    if (gui.returnAddFlag())
                    {
                        typeFlag=1;
                        line[0]=gui.returnId();
                        line[1]=gui.returnDate();
                        line[2]= String.valueOf(false);
                        gui.mixFlag();
                        flag=false;
                    }
                    else if (gui.returnSearchFlag())
                    {
                        line[0]=gui.returnId();
                        typeFlag=2;
                        gui.mixFlag();
                        flag=false;
                    }
                    else if (gui.returnDeleteFlag())
                    {
                        line[0]=gui.returnId();
                        typeFlag=3;
                        gui.mixFlag();
                        flag=false;
                    }
                    else if (gui.returnUpdateFlag())
                    {
                        line[0]=gui.returnId();
                        typeFlag=4;
                        line[1]=gui.returnDate();
                        line[2]= String.valueOf(gui.returnRent());
                        gui.mixFlag();
                        flag=false;
                    }
                    else if (gui.returnFlag())
                    {
                        line[0]="exit";
                        typeFlag=5;
                        gui.mixFlag();
                        break;
                    }*/
                    else
                    {
                        os.println(-1);
                        os.flush();
                    }
                    System.out.print("");
                }

                os.println(typeFlag);
                os.flush();
                os.println(line[0]);
                os.flush();
                if (line[0].equals("exit"))
                    break;
                os.println(line[1]);
                os.flush();
                os.println(line[2]);
                os.flush();
                flag=true;
                //从Server读入字符串，并打印到屏幕上
                if (typeFlag==2) {
//                    gui.testData(is.readLine(),is.readLine(),is.readLine());
                    System.out.println(is.lines());
                }
            }//继续循环
        os.close(); //关闭输出流
            is.close(); //关闭输入流
            cs.close(); //关闭 client socket
            System.out.println("client is closed...");
        }
    public static void main(String[] args) throws Exception {


        System.out.println("断开连接");
        System.exit(0);
    }
}

